/* CONSTANCE ------------------------------------------------------------------------------------------------------- */
var IMPEDIMENT_RULES = {
    description: {
        required: true,
        minlength: 5,
        maxlength: 500
    }
};
var IMPEDIMENT_MESSAGES = {
    description: {
        required: '<span class="text-error">Please enter the description.</span>',
        minlength: '<span class="text-error">The description is too short.</span>',
        maxlength: '<span class="text-error">Please keep the description shorter than 500 characters.</span>'
    }
};
/* end - CONSTANCE ------------------------------------------------------------------------------------------------- */

$.date = function (dateObject) {
    var d = new Date(dateObject);
    var day = d.getDate();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    var date = day + "/" + month + "/" + year;

    return date;
}

function getImpediments(projectId, teamId) {
    console.log('get impediments.');
    $('.impediments').html('');

    if (!$('.sprint-selector [name="sprint"]').val()) {
        $('.impediments').html('<div class="text-center text-error" style="width: 240px;">Current sprint not found! Can not load or create impediment.</div>');
        return false;
    }

    if (!projectId || !teamId) {
        return false;
    }

    var params = new Object();
    params.proId = projectId;
    params.teamId = teamId;

    $.ajax({
        url: SHOW_RETRO_CTRL,
        dataType: 'json',
        data: params,
        success: function (data) {
            $.each(data, function (id, impediment) {
                var $item = $('.rest-in-peace section.impediment').clone();
                var id = impediment[3];
                var description = impediment[0];
                var owner = impediment[5].firstName + " " + impediment[5].lastName.split(" ")[0];
                var date = $.date(impediment[4]);
                var status = impediment[2];

                $item.attr('id', id);
                $item.addClass(status);
                $item.find('[name="description"]').html(description);
                $item.find('[name="owner"]').html(owner);
                $item.find('[name="date"]').html(date);
                $item.find('[name="status"]').addClass(status);

                $item.appendTo('.impediments');
            });
        }
    });
}

function changeImpedimentStatus(button) {
    var impedimentId = button.parents('.impediment').attr('id');

    if (confirm("Are you sure to change?")) {
        $impediment = button.parents('.impediment');

        $impediment.removeClass('Unsolved');
        $impediment.addClass('Solved');

        button.removeClass('Unsolved');
        button.addClass('Solved');
        $.ajax({
            url: UPDATE_RETRO_TYPE_CTRL,
            dataType: 'json',
            data: {id: impedimentId}
        });
    }
}

function updateImpediment(button) {
    var $impediment = button.parents('.impediment');
    var $form = button.parents('.impediment-form');
    var params = new Object();
    params.id = $impediment.attr('id');
    params.de = $form.find('[name="description"]').val();

    $.ajax({
        url: UPDATE_RETRO_CTRL,
        type: 'post',
        dataType: 'json',
        data: params,
        success: function (data) {
            $impediment.find('[name="description"]').html(data.retro.description);
        }
    });
}

function deleteImpediment(impedimentId) {
    $.ajax({
        url: DETELE_RETRO_CTRL,
        dataType: 'json',
        data: {id: impedimentId},
        success: function (data) {
            getImpe();
        }
    });
}

$.fn.pressEnter = function (fn) {
    return this.each(function () {
        $(this).bind('enterPress', fn);
        $(this).keyup(function (e) {
            if (e.keyCode == 13) {
                $(this).trigger("enterPress");
            }
        })
    });
};

function createImpediment(params) {
    $.ajax({
        url: CREATE_RETRO_CTRL,
        type: 'post',
        dataType: 'json',
        data: params,
        success: function (data) {
            getImpediments(params.pId, params.tId);
        }
    });
}

function showCreateImpedimentForm() {
    var $form = $('.impediment-form');
    var $formParent = $form.parent();
    var projectId = $('.main-selector [name="project"]').val();
    var teamId = $('.main-selector [name="team"]').val();
    var sprintId = $('.sprint-selector [name="sprint"]').val();

    if ($formParent.is('section.impediment')) {
        $formParent.find('[name="description"]').fadeIn();
    }

    if (projectId && teamId && sprintId) {
        // show label
        $form.find('[name="descLabel"]').show();
        // hide save button
        $form.find('[name="save"]').hide();
        // show create button
        $form.find('[name="create"]').show();
        // show form on top of user stories
        $form.prependTo('.impediments');
        // show the form
        $form.show(300);
    }
}

function hideImpedimentForm() {
    var $form = $('.impediment-form');
    var $formParent = $form.parent();

    if ($formParent.is('section.impediment')) {
        // show previous description
        $formParent.find('[name="description"]').fadeIn();
    }

    $form.hide(300);
    setTimeout(function () {
        $form.appendTo('.rest-in-peace');
    }, 300);
}

function getDataFromImpediment(impediment) {
    var obj = new Object();

    obj.id = impediment.attr('id');
    obj.description = impediment.find('[name="description"]').html();
    obj.status = impediment.find('[name="status"]').html();
    obj.pId = $('.main-selector [name="project"]').val();
    obj.tId = $('.main-selector [name="team"]').val();

    return obj;
}

function setImpedimentForm(data) {
    var $form = $('.impediment-form');

    $form.find('[name="description"]').val(data.description);
    $form.find('[name="project"]').val(data.project);
    $form.find('[name="team"]').val(data.team);
}

function resetImpedimentForm() {
    var obj = new Object();

    obj.description = "";
    obj.pId = $('.main-selector [name="project"]').val();
    obj.tId = $('.main-selector [name="team"]').val();

    setImpedimentForm(obj);
}

function getImpedimentFormData() {
    var $form = $('.impediment-form');
    var obj = $form.serializeObject();
    var data = new Object();

    data.id = '';
    data.description = obj.description.trim();
    data.pId = $('.main-selector [name="project"]').val();
    data.tId = $('.main-selector [name="team"]').val();

    return data;
}

function showEditImpedimentForm(button) {
    var $form = $('.impediment-form');
    var $formParent = $form.parent();
    var $impediment = button.parents('section.impediment');

    // hide label
    $form.find('[name="descLabel"]').hide();
    // hide create button
    $form.find('[name="create"]').hide();
    // show save button
    $form.find('[name="save"]').show();
    // hide current description
    $impediment.find('[name="description"]').hide();
    // show the form
    $form.appendTo($impediment);
    $form.fadeIn();

    if ($formParent.is('section.impediment')) {
        // show previous content
        $formParent.find('[name="description"]').fadeIn();
    }
}